/* entity */
import Field from '@model/entity/Field'
import ProductCatalog, { ProductCatalogType } from '@model/entity/ProductCatalog'
import { QualityRuleForm, QualityRuleOriginal, QualityRuleQualityTime, QualityRuleSearchFrom } from '@model/entity/QualityRule'
/* enum */
import { ProductTypeFieldNameMappingEnum } from '@model/enum/FieldMappingEnum'
/* hooks */
import { useQualityRuleFormGroupFields } from '@hooks/useQualityRule'
/* service */
import { isQualityRuleField, isQualityRuleStartTimeField, isQualityTimeField, getFieldSettingDataType } from '@service/FieldService'
/* util */
import * as _ from 'lodash'
import { isEmpty, isNumber } from '@src/util/type'
/* vue */
import { ComputedRef } from 'vue'
import { QualityRuleTimeTypeEnum } from '@model/enum/QualityRuleEnum'

/**
 * @description: 服务端质保规则数据转成表单质保规则数据
 * @param {QualityRuleOriginal} qualityRuleRemoteData
 * @return {QualityRuleForm} 表单质保规则数据
 */
export function convertQualityRuleRemoteDataToFormQualityRuleFieldValue(
  qualityRuleRemoteData: QualityRuleOriginal | ProductCatalogType | undefined,
  fields: Field[]
): QualityRuleForm {
  // 质保时长数据
  const qualityDuration = isNumber(qualityRuleRemoteData?.qualityDuration) ? qualityRuleRemoteData?.qualityDuration : qualityRuleRemoteData?.qualityDuration || ''
  return {
    // 质保起算时间
    [ProductTypeFieldNameMappingEnum.QualityStartTime]: getFieldSettingDataType(fields) == 1 ? qualityRuleRemoteData?.qualityFiledName : qualityRuleRemoteData?.qualityStartDate || '',
    // 质保时长
    [ProductTypeFieldNameMappingEnum.QualityTime]: {
      // 质保时长
      qualityTimeNumber: qualityDuration,
      // 质保时长 (月/年)
      qualityTimeType: qualityRuleRemoteData?.qualityUnit || ''
    },
    [ProductTypeFieldNameMappingEnum.QualityFiledName]: qualityRuleRemoteData?.qualityFiledName
  }
  
}

/**
 * @description: 表单质保规则数据转成服务端质保规则数据
 * @param {QualityRuleForm} QualityRuleForm
 * @return {QualityRuleOriginal} 服务端质保规则数据
 */
export function convertFormQualityRuleFieldValueToQualityRuleRemoteData(
  formQualityFieldValue: QualityRuleForm, 
  fields: Field[]
): QualityRuleOriginal {
  // 质保时长数据
  const qualityTimeData = formQualityFieldValue?.[ProductTypeFieldNameMappingEnum.QualityTime] || {}
  return {
    // 质保起算时间
    qualityStartDate: formQualityFieldValue?.[ProductTypeFieldNameMappingEnum.QualityStartTime] || '',
    // 质保时长
    qualityDuration: Number(qualityTimeData?.qualityTimeNumber),
    // 质保时长 (月/年)
    qualityUnit: qualityTimeData?.qualityTimeType || '',
    // 质保关联产品对应的fieldName
    qualityFiledName: getFieldSettingDataType(fields) == 1 ? formQualityFieldValue?.[ProductTypeFieldNameMappingEnum.QualityStartTime] : ''
  }
  
}

/**
 * @description: 表单质保规则数据转成服务端搜索表单质保规则数据
 * @param {QualityRuleForm} QualityRuleForm
 * @return {QualityRuleOriginal} 服务端质保规则数据
 */
export function convertFormQualityRuleFieldValueToQualityRuleSearchFormData(
  formQualityFieldValue: QualityRuleForm
): QualityRuleSearchFrom {
  
  // 质保时长数据
  const qualityTimeData: QualityRuleQualityTime = formQualityFieldValue?.[ProductTypeFieldNameMappingEnum.QualityTime] || {}
  // 质保起算时间
  const qualityStartTime: string = formQualityFieldValue?.[ProductTypeFieldNameMappingEnum.QualityStartTime] || ''
  
  return {
    // 质保起算时间
    qualityStartDate: qualityStartTime ? [qualityStartTime] : [],
    // 关联质保起算时间
    qualityFiledName: qualityStartTime ? [qualityStartTime] : [],
    // 质保时长
    qualityDuration: Number(qualityTimeData?.qualityTimeNumber),
    // 质保时长 (月/年)
    qualityUnit: qualityTimeData?.qualityTimeType || ''
  }
  
}

/**
 * @description 产品类型质保规则字段展开
 * @return {Field[]} 展开的字段列表 
 */
export function smoothQualityRuleField(fields: Field[]): Field[] {
  const originFields: Field[] = _.cloneDeep(fields)
  // 质保规则字段索引
  const qualityRuleFieldIndex: number = originFields.findIndex((field: Field) => isQualityRuleField(field))
  // 无质保规则字段则返回
  if (qualityRuleFieldIndex < 0) return originFields
  
  // 质保规则字段
  const qualityRuleField: Field = fields[qualityRuleFieldIndex]
  // 质保规则字段列表
  const qualityRuleFields: ComputedRef<Field[]> = useQualityRuleFormGroupFields(qualityRuleField)
  const qualityRuleFieldsValue: Field[] = qualityRuleFields.value
  
  // 替换质保字段为质保字段列表(平铺)
  fields
  .splice(
    qualityRuleFieldIndex,
    1,
    qualityRuleFieldsValue[1], qualityRuleFieldsValue[2]
  )
  
  return (
    fields.filter(field => Boolean(field))
  )
}

/**
 * @description 质保时长字段是否为空
 * @param {QualityRuleQualityTime} qualityRuleFieldTimeData 质保时长表单数据
 * @return {Boolean}
 */
export function isQualityRuleTimeFieldEmpty(qualityRuleFieldTimeData: QualityRuleQualityTime): boolean {
  return !qualityRuleFieldTimeData?.qualityTimeNumber || isEmpty(qualityRuleFieldTimeData?.qualityTimeType)
}

/**
 * @description 质保时长字段 时长数值是否为空
 * @param {QualityRuleQualityTime} qualityRuleFieldTimeData 质保时长表单数据
 * @return {Boolean}
 */
export function isQualityRuleTimeFieldQualityTimeNumberEmpty(qualityRuleFieldTimeData: QualityRuleQualityTime): boolean {
  return !qualityRuleFieldTimeData?.qualityTimeNumber
}

/**
 * @description 质保时长字段 时长单位是否为空
 * @param {QualityRuleQualityTime} qualityRuleFieldTimeData 质保时长表单数据
 * @return {Boolean}
 */
export function isQualityRuleTimeFieldQualityTimeTypeEmpty(qualityRuleFieldTimeData: QualityRuleQualityTime): boolean {
  return isEmpty(qualityRuleFieldTimeData?.qualityTimeType)
}

/**
 * @description 质保时长字段 时长类型是否为年
 * @param {QualityRuleTimeTypeEnum} value 质保时长 时长类型
 * @return {Boolean}
 */
export function isQualityRuleTimeYear(value: QualityRuleTimeTypeEnum | string): boolean {
  return value === QualityRuleTimeTypeEnum.Year
}

/**
 * @description 质保时长字段 时长类型是否为月
 * @param {QualityRuleTimeTypeEnum} value 质保时长 时长类型
 * @return {Boolean}
 */
export function isQualityRuleTimeMonth(value: QualityRuleTimeTypeEnum | string): boolean {
  return value === QualityRuleTimeTypeEnum.Month
}


export default {
  convertQualityRuleRemoteDataToFormQualityRuleFieldValue,
  convertFormQualityRuleFieldValueToQualityRuleRemoteData,
  isQualityRuleTimeFieldEmpty,
  isQualityRuleTimeYear,
  isQualityRuleTimeMonth
}